/**
 * Copyright © 1998-2016, Glodon Inc. All Rights Reserved.
 */
package autotest.web.framework.plugin.wizards;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.ILaunchShortcut2;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;

public class SuRenLauncher implements ILaunchShortcut2
{
	private ILaunchConfiguration findExistingLaunchConfiguration(
			ILaunchConfigurationWorkingCopy temporary, String mode)
			throws InterruptedException, CoreException
	{
		List<ILaunchConfiguration> candidateConfigs = findExistingLaunchConfigurations(temporary);

		int candidateCount = candidateConfigs.size();
		if (candidateCount == 0)
		{
			return null;
		}
		if (candidateCount == 1)
		{
			return (ILaunchConfiguration) candidateConfigs.get(0);
		}
		ILaunchConfiguration config = chooseConfiguration(candidateConfigs,
				mode);
		if (config != null)
		{
			return config;
		}
		return null;
	}

	private ILaunchConfiguration chooseConfiguration(
			List<ILaunchConfiguration> configList, String mode)
			throws InterruptedException
	{
		IDebugModelPresentation labelProvider = DebugUITools
				.newDebugModelPresentation();
		ElementListSelectionDialog dialog = new ElementListSelectionDialog(
				getShell(), labelProvider);
		dialog.setElements(configList.toArray());
		dialog.setTitle("JUnitMessages.JUnitLaunchShortcut_message_selectConfiguration");
		if (mode.equals("debug"))
		{
			dialog.setMessage("JUnitMessages.JUnitLaunchShortcut_message_selectDebugConfiguration");
		}
		else
		{
			dialog.setMessage("JUnitMessages.JUnitLaunchShortcut_message_selectRunConfiguration");
		}
		dialog.setMultipleSelection(false);
		int result = dialog.open();
		if (result == 0)
		{
			return (ILaunchConfiguration) dialog.getFirstResult();
		}
		throw new InterruptedException();
	}

	private Shell getShell()
	{
		return JavaPlugin.getActiveWorkbenchShell();
	}

	private List<ILaunchConfiguration> findExistingLaunchConfigurations(
			ILaunchConfigurationWorkingCopy temporary) throws CoreException
	{
		ILaunchConfigurationType configType = temporary.getType();

		ILaunchConfiguration[] configs = getLaunchManager()
				.getLaunchConfigurations(configType);
		String[] attributeToCompare = new String[]
		{};// getAttributeNamesToCompare();

		ArrayList<ILaunchConfiguration> candidateConfigs = new ArrayList(
				configs.length);
		for (ILaunchConfiguration config : configs)
		{
			candidateConfigs.add(config);
			// if (hasSameAttributes(config, temporary, attributeToCompare))
			// {
			// candidateConfigs.add(config);
			// }
		}
		return candidateConfigs;
	}

	@Override
	public void launch(ISelection selection, String mode)
	{
		System.out.println(selection);
		if (selection instanceof ITreeSelection)
		{
			if (((ITreeSelection) selection).getFirstElement() instanceof IJavaProject)
			{
				IJavaProject project = ((IJavaProject) ((ITreeSelection) selection)
						.getFirstElement());
				try
				{
					IType type = project
							.findType("org.suren.autotest.web.framework.core.suite.SuiteRunner");
					System.out.println(type);

					ILaunchConfigurationWorkingCopy temparary = createLaunchConfiguration(type);
					ILaunchConfiguration config = findExistingLaunchConfiguration(
							temparary, mode);
					if (config == null)
					{
						config = temparary.doSave();
					}
					DebugUITools.launch(config, mode);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		}
	}

	protected ILaunchConfigurationWorkingCopy createLaunchConfiguration(
			IJavaElement element) throws CoreException
	{

		ILaunchConfigurationType configType = getLaunchManager()
				.getLaunchConfigurationType(getLaunchConfigurationTypeId());
		ILaunchConfigurationWorkingCopy wc = configType.newInstance(null,
				getLaunchManager().generateLaunchConfigurationName("testName"));

		wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
				element.getJavaProject().getElementName());
		wc.setAttribute("org.eclipse.jdt.junit.KEEPRUNNING_ATTR", false);
		// wc.setAttribute("org.eclipse.jdt.junit.CONTAINER",
		// containerHandleId);
		// wc.setAttribute("org.eclipse.jdt.junit.TEST_KIND", testKindId);
		return wc;
	}

	private ILaunchManager getLaunchManager()
	{
		return DebugPlugin.getDefault().getLaunchManager();
	}

	protected String getLaunchConfigurationTypeId()
	{
//		return "org.eclipse.jdt.junit.launchconfig";
		return "org.eclipse.jdt.launching.localJavaApplication";
	}

	@Override
	public void launch(IEditorPart arg0, String arg1)
	{
		System.out.println(arg0);
	}

	@Override
	public ILaunchConfiguration[] getLaunchConfigurations(ISelection arg0)
	{
		System.out.println(arg0);
		return null;
	}

	@Override
	public ILaunchConfiguration[] getLaunchConfigurations(IEditorPart arg0)
	{
		return null;
	}

	@Override
	public IResource getLaunchableResource(ISelection arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IResource getLaunchableResource(IEditorPart arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

}
